/**
 * gvSIG. Desktop Geographic Information System.
 *
 * Copyright (C) 2007-2012 gvSIG Association.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 *
 * For any additional information, do not hesitate to contact us
 * at info AT gvsig.com, or visit our website www.gvsig.com.
 */
package org.gvsig.titleblock.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

import org.gvsig.app.ApplicationLocator;
import org.gvsig.app.ApplicationManager;
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
import org.gvsig.titleblock.IFFrameItem;
import org.gvsig.titleblock.IFFrameToTB;
import org.gvsig.titleblock.IFFrames;
import org.gvsig.titleblock.ITBManager;
import org.gvsig.tools.ToolsLocator;
import org.gvsig.tools.i18n.I18nManager;
import org.gvsig.tools.persistence.PersistenceManager;
import org.gvsig.tools.persistence.PersistentState;
import org.gvsig.tools.persistence.exception.PersistenceException;

/**
 * Represents a FFrame transformed into a TitleBlock
 * 
 * @author Violeta Carlos
 * @version $Id$
 */
public class FFrameToTB implements IFFrameToTB {
	
	private IFFrames fframesBox;
	private ITBManager manager;
	private I18nManager i18nmanager;
	private PersistenceManager persistManager;
	private ApplicationManager applicationManager;
	
	/**
	 * Sets the variables
	 * @param manager
	 */
	public FFrameToTB(ITBManager manager) {
		this.manager = manager;
		this.i18nmanager = ToolsLocator.getI18nManager();
		this.persistManager = ToolsLocator.getPersistenceManager();
		this.applicationManager = ApplicationLocator.getManager(); 
	}
	
	public void initilize(IFFrames fframes) {
		this.fframesBox=fframes;
	}
	
	/**
	 * Creates the frame and saves it in GVB format
	 */
	public void framesToGVB(File fAux, HashMap<String, IFFrameItem> selecteds) {
		if (selecteds.size()>0){
			setTag(selecteds, "title");
			setTag(selecteds, "subtitle");
			setTag(selecteds, "picture1");
			setTag(selecteds, "picture2");
			setTag(selecteds, "author");
			setTag(selecteds, "date");
		}
		
		try {
			save(fAux);
		} catch (PersistenceException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public ITBManager getManager() {
		return manager;
	}
	
	/**
	 * Adds the tags to the FFrame items
	 * @param selecteds
	 * @param s
	 */
	private void setTag(HashMap<String, IFFrameItem> selecteds, String s){
		IFFrameItem titleItem=selecteds.get(s);
		IFFrame frame=titleItem.getFFrame();
		if (frame!=null)
			frame.setTag(s);
	}
	
	/**
	 * Saves the TitleBlock
	 * @param file
	 * @throws PersistenceException
	 * @throws IOException
	 */
	private void save(File file) throws PersistenceException, IOException{
		PersistentState state1 = persistManager.getState(fframesBox);
		OutputStream out = new FileOutputStream(file);
		persistManager.saveState(state1, out);
		out.close();
	}

	/** 
	 * Returns the array of the picture items
	 */
	public IFFrameItem[] getPictureFrames() {
		return fframesBox.getPictureFrames();
	}

	/**
	 * Returns the array of text items
	 */
	public IFFrameItem[] getTextFrames() {
		return fframesBox.getTextFrames();
	}
	
}
